<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 *
 * @abstract 给tomrepair写的获取散客支付记录的接口
 * @author sqw 20161201
 *
 */
class PaymentRecord extends CI_Controller
{

    function __construct()
    {
        parent::__construct();
        //$this->output->enable_profiler(TRUE);
        $this->load->model('ajax_model/ajax_two_model', 'ajaxTwoModel');
    }

    /**
     * 添加散客的支付记录
     * 需要参数 参数orderId（订单编号），money（金额），currency（币种），transId（交易号），country（国家编码），site（来源站点简写），payType（支付方式）
     */
    public function index()
    {
 
        $gp = gp();

        //以下是调试日志 BEGIN
        $logInfo = array('file_path' => __FILE__, 'fun' => __FUNCTION__, 'reffer' => (!empty($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : 'Not Reffer'), 'data' => $gp);
        log_output($logInfo);
        if (empty($gp['orderId']) || empty($gp['money']) ||  empty($gp['transId']) || empty($gp['country']) || empty($gp['site']) || empty($gp['payType'])) {
            die(json_encode(array('success' => -4, 'message' => 'Incomplete parameter!')));
        }
        //以上是调试日志 END
        $order_num = $gp['orderId'];
        //客户id，根据email，查出cutomerid

        $order = $this->common_model->selectFirst('mt_order', array('order_num' => $order_num));
        $customer = $this->common_model->selectFirst('mt_customers', array('email' => $order['consumer_email']));
        if ($order==null) {
            die(json_encode(array('success' => -5, 'message' => 'Order does not exist!')));
        }
        $dataRecord = array(
            'customers_id' => ($customer != null) ? $customer['id'] : -1,//如果没有查到id则返回-1，-1为异常单
            'time' => time(),//时间
            'orderId' => $order_num,
            'money' => $gp['money'],//金额
            'currency' => $gp['currency'],//币种
            'transactionId' => $gp['transId'],//交易号
            'account' => $order['consumer_email'],//客户账户,
            'country_code' => $gp['country'],//国家代码
            'sourceType' => 2, //来源，1为OMS自身扣款记录，2tomrepair网站接口传入数据，3为导入数据
            'sourceSite' => $gp['site'],//站点来源，空为平台自身扣款记录，传入站点见称，如es，de等
            'payType' => $gp['payType'] //支付方式，1=redsys 2=paypal
        );
        //print_r($dataRecord);
        $res = $this->common_model->insert('mt_payment_record', $dataRecord);
        if ($res > 0) {
            die(json_encode(array('success' => 1, 'message' => 'successfully!')));
        } else {
            die(json_encode(array('success' => 0, 'message' => 'failed!')));
        }
    }

}
